<template>
  <div>
    <my-form-drawer
      ref="drawer"
      :title="title"
      :width="350"
      :visible="visible"
      @ok="save('ruleForm')"
      @cancel="onClose"
      :footer="true"
      :zIndex="15"
    >
      <template>
        <a-form-model ref="ruleForm" :model="form" :rules="rules">
          <a-form-model-item ref="name" label="用户名">
            <a-input :disabled="true" v-model="user.name" />
          </a-form-model-item>
          <a-form-model-item label="原密码" prop="oldPassword">
            <a-input-password v-model="form.oldPassword"> </a-input-password>
          </a-form-model-item>
          <a-form-model-item label="新密码" prop="password">
            <a-input-password v-model="form.password"></a-input-password>
          </a-form-model-item>
          <a-form-model-item label="确认密码" prop="passwords">
            <a-input-password v-model="form.passwords"></a-input-password>
          </a-form-model-item>
        </a-form-model>
      </template>
    </my-form-drawer>
  </div>
</template>
<script>
import MyFormDrawer from "@/components/drawer/MyFormDrawer";
import { mapGetters } from "vuex";
import { rule } from "@/utils/validateRules";
import { changePwd } from "@/services/user";
import { encrypt } from "@/utils/util";

export default {
  name: "modifyPwd",
  components: { MyFormDrawer },
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    ...mapGetters("account", ["user"])
  },
  data() {
    return {
      title: "修改密码",
      oldPwd: false,
      newPwd: false,
      definePwd: false,
      form: {
        type: 2,
        oldPassword: "",
        password: "",
        passwords: ""
      },
      rules: {
        oldPassword: [
          {
            required: true,
            message: "请输入原密码",
            trigger: "blur"
          }
        ],
        password: [
          { required: true, message: "密码不能为空", trigger: "blur" },
          { min: 8, message: "密码长度至少为8位", trigger: ["change", "blur"] },
          { trigger: ["change", "blur"], validator: rule.validatePwd }
        ],
        passwords: [
          {
            required: true,
            message: "请确认密码",
            trigger: "blur"
          },
          {
            trigger: "blur",
            validator: (rule, value, callback) =>
              this.validatePass2(rule, value, callback)
          }
        ]
      }
    };
  },
  methods: {
    validatePass2(rule, value, callback) {
      if (value !== this.form.password) {
        callback(new Error("两次密码不一致"));
      } else {
        callback();
      }
    },
    onClose() {
      (this.form = {
        type: 2,
        oldPassword: "",
        password: "",
        passwords: ""
      }),
        this.$emit("closeForm", false);
    },
    save(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          changePwd(encrypt(this.form.password), 2, encrypt(this.form.oldPassword)).then(res => {
            if (res.data.code == 0) {
              this.$message.success("修改成功");
              this.onClose();
            }
          });
        } else {
          return false;
        }
      });
    }
  }
};
</script>

<style scoped></style>
